﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using GAM_Web_Designer_Pro.Properties;

namespace GAM_Web_Designer_Pro {
    public static class ProjectTemplateAccess {
        private static string _dataDir = Application.StartupPath + GAM_Web_Designer_Pro.Properties.Settings.Default.ProjectTemplateDir;

        /// <summary>
        /// Saves the project template.
        /// </summary>
        /// <param name="temp">The template to save.</param>
        public static void SaveProjectTemplate( ProjectTemplate temp ) {
            try
            {
                FileStream stream = File.Create( _dataDir + @"\" + temp.Name + GAM_Web_Designer_Pro.Properties.Settings.Default.ProjTempExt );
                BinaryFormatter formatter = new BinaryFormatter();

                formatter.Serialize( stream, temp );
                stream.Close();

            }
            catch (System.Exception ex)
            {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
        }

        /// <summary>
        /// Loads a projest template.
        /// </summary>
        /// <param name="path">The path to the .projtemp file.</param>
        public static ProjectTemplate LoadProjectTemplate( string path ) {
            ProjectTemplate template = new ProjectTemplate();

            try {

                FileStream stream = File.Open( path, FileMode.Open );
                BinaryFormatter formatter = new BinaryFormatter();

                template = (ProjectTemplate)formatter.Deserialize( stream );
                stream.Close();

                return template;
            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }

            return template;
        }

        /// <summary>
        /// Loads all the project templates in the ProjectTemplate directory.
        /// </summary>
        public static List<ProjectTemplate> LoadAllProjectTemplates() {
            List<ProjectTemplate> temps = new List<ProjectTemplate>();

            try {

                string[] files = Directory.GetFiles( _dataDir );
                ProjectTemplate temp = new ProjectTemplate();
                FileStream stream;
                BinaryFormatter formatter = new BinaryFormatter();

                for ( int i = 0; i < files.Length; i++ ) {
                    if ( files[ i ].EndsWith( Settings.Default.ProjTempExt ) ) {
                        stream = File.Open( files[ i ], FileMode.Open );
                        temp = (ProjectTemplate)formatter.Deserialize( stream );
                        temps.Add( temp );
                        stream.Close();
                    }
                }

                return temps;

            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }

            return temps;
        }
    }
}
